package com.xes.apps.ams.client.rd60n.jiecard.cn;

import struct.ArrayLengthMarker;
import struct.StructClass;
import struct.StructField;

@StructClass
public class CmdData {
	public static final byte CMD_ID_REQUEST = (byte) 0x21; //
	public static final byte CMD_ID_ANTICOLL = (byte) 0x22; //
	public static final byte CMD_ID_SELECT = (byte) 0x23; //
	public static final byte CMD_ID_AUTHENT = (byte) 0x24; //
	public static final byte CMD_ID_READ = (byte) 0x25; //
	public static final byte CMD_ID_WRITE = (byte) 0x26; //
	public static final byte CMD_ID_HALT = (byte) 0x27; // ֹͣ
	public static final byte CMD_ID_ACTIVE_CARD = (byte) 0x28; //

	public static final byte CMD_ID_BEEP = (byte) 0x31; //
	public static final byte CMD_ID_DISP = (byte) 0x32; // ʾ

	public static final byte CMD_ID_SETTIME = (byte) 0x33; //
	public static final byte CMD_ID_SETIP = (byte) 0x34; //
	public static final byte CMD_ID_SETSERV = (byte) 0x35; //
	public static final byte CMD_ID_SET_LCD_BL = (byte) 0x36; //
	public static final byte CMD_ID_SET_LCD_TITLE = (byte) 0x37; //
	public static final byte CMD_ID_RELAY_ON = (byte) 0x38; //
	public static final byte CMD_ID_SETDNS = (byte) 0x39; //

	public static final byte CMD_ID_SET_LCD_BL_TMP = (byte) 0x3A; //
	public static final byte CMD_ID_SET_XDF = (byte) 0x3B; //

	public static final byte CMD_ID_DEVINFO = (byte) 0x41; //

	public static final byte CMD_ID_GETTIME = (byte) 0x83; //
	public static final byte CMD_ID_GETIP = (byte) 0x84; //
	public static final byte CMD_ID_GETSERV = (byte) 0x85; //
	public static final byte CMD_ID_GETVER = (byte) 0x86; //
	public static final byte CMD_ID_GETDNS = (byte) 0x87; //
	public static final byte CMD_ID_MEM_STATUS = (byte) 0x88; //
	public static final byte CMD_ID_GET_XDF = (byte) 0x89; //

	public static final byte CMD_ID_GET_KEY_EX = (byte) 0x91; //

	public static final byte CMD_ID_CARD_AUTO_UP = (byte) 0xA1; //
	public static final byte CMD_ID_CARD_CONFIRM = (byte) 0xA2; //
	public static final byte CMD_ID_STATUS_EXKEY = (byte) 0xA3; //

	public static final byte CMD_ID_SFLASH_INFO = (byte) 0xD1; //
	public static final byte CMD_ID_SFLASH_ERASE = (byte) 0xD2; //
	public static final byte CMD_ID_SFLASH_WRITE = (byte) 0xD3; //
	public static final byte CMD_ID_SFLASH_READ = (byte) 0xD4; //

	public static final byte CMD_ID_PARAM_RESET = (byte) 0xED; //
	public static final byte CMD_ID_RESTART = (byte) 0xEE; //

	public static final byte MSG_TYPE_MAX = (byte) 0xFF;

	@StructField(order = 0)
	public byte cmd_flag;

	@StructField(order = 1)
	public byte[] dev_sn = new byte[4];

	@StructField(order = 2)
	@ArrayLengthMarker(fieldName = "data")
	public byte cmd_size;

	@StructField(order = 3)
	public byte cmd_id;

	@StructField(order = 4)
	public byte cmd_result;

	@StructField(order = 5)
	public byte[] data;

	@StructField(order = 6)
	public byte crc;

	CmdData(byte[] cmd_data, byte _id) {
		if (null != cmd_data) {
			data = new byte[cmd_data.length];
			System.arraycopy(cmd_data, 0, data, 0, data.length);
			cmd_flag = (byte) 0xAB;
			for (int i = 0; i < 4; ++i)
				dev_sn[i] = 0;
			cmd_result = 0;
			cmd_size = (byte) (cmd_data.length + 9);
			cmd_id = _id;
			
			crc = 0;
			crc ^= cmd_flag;
			for (int i = 0; i < 4; ++i)
				crc ^= dev_sn[i];
			crc^= cmd_size;
			crc^= cmd_id;
			crc^= cmd_result;
			for(int i = 0 ; i< data.length; ++i)
				crc ^= data[i];
			
		}
	}
}